﻿using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Linq;

namespace PageAdmin.Utils
{

    /// <summary>
    /// 插件帮助类
    /// </summary>
    public class PluginHelper
    {

        /// <summary>
        /// 检测插件菜单权限
        /// </summary>
        /// <param name="user"></param>
        /// <param name="actionDescriptor"></param>
        /// <param name="pluginName"></param>
        /// <returns></returns>
        public static bool CheckMenuItemAuthority(CurrentUser user, ActionInfo actionDescriptor, string pluginName)
        {
            string configFile = "/Plugins/" + pluginName + "/MenuItemAuthority.Config";
            if (!IOHelper.FileIsExists(configFile))
            {
                return true;//文件不存在表示还没有安装
            }
            IEnumerable<PluginMenuItemDescriptor> pluginMenuItemDescriptorList = GetPluginMenuItemDescriptor(configFile, "configuration/adminMenu");
            var item = pluginMenuItemDescriptorList.Where(c => c.ActionDescriptor.NameSpace == actionDescriptor.NameSpace && c.ActionDescriptor.ControllerName == actionDescriptor.ControllerName && c.ActionDescriptor.ActionName == actionDescriptor.ActionName && c.ActionDescriptor.RequestType == actionDescriptor.RequestType).FirstOrDefault();
            if (item == null) //找不到这返回true
            {
                return true;
            }
            return true;
        }

        /// <summary>
        /// 检测当前用户权限
        /// </summary>
        /// <param name="user"></param>
        /// <param name="pluginMenuItemDescriptor"></param>
        /// <returns></returns>
        private bool CheckUserAuthority(CurrentUser user, PluginMenuItemDescriptor pluginMenuItemDescriptor)
        {
            //检测角色
            if (pluginMenuItemDescriptor.RoleIds.Count > 0)
            {
                if (!pluginMenuItemDescriptor.RoleIds.Contains(user.RoleId))
                {
                    return false;
                }
            }

            //检测用户组
            if (pluginMenuItemDescriptor.MemberGroupIds.Count > 0)
            {
                if (!pluginMenuItemDescriptor.MemberGroupIds.Contains(user.MemberGroupId))
                {
                    return false;
                }
            }

            //检测用户
            if (pluginMenuItemDescriptor.Uids.Count > 0)
            {
                if (!pluginMenuItemDescriptor.Uids.Contains(user.Uid))
                {
                    return false;
                }
            }
            //检测部门
            if (pluginMenuItemDescriptor.DepartmentIds.Count > 0)
            {
                if (!pluginMenuItemDescriptor.DepartmentIds.Contains(user.DepartmentId))
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 获取配置信息转为集合
        /// </summary>
        /// <param name="configFile"></param>
        /// <param name="nodeName"></param>
        private static IEnumerable<PluginMenuItemDescriptor> GetPluginMenuItemDescriptor(string configFile, string nodeName)
        {
            IList<PluginMenuItemDescriptor> enumerable = new List<PluginMenuItemDescriptor>();

            XmlDocument doc = XmlHelper.LoadXmlDocument(configFile, true);
            XmlElement xe;
            XmlNode parentNode = doc.SelectSingleNode(nodeName);
            if (parentNode == null)
            {
                return enumerable;
            }
            XmlNodeList xnlist = doc.SelectSingleNode(nodeName).ChildNodes;
            foreach (XmlNode node in xnlist)
            {
                PluginMenuItemDescriptor pluginMenuItemDescriptor = new PluginMenuItemDescriptor();
                if (node.NodeType != XmlNodeType.Element) { continue; }
                xe = (XmlElement)(node);
                SetControllerActionDescriptor(xe, pluginMenuItemDescriptor);
                enumerable.Add(pluginMenuItemDescriptor);
            }
            return enumerable;
        }

        /// <summary>
        /// 设置控制器
        /// </summary>
        /// <param name="xe"></param>
        /// <param name="pluginMenuItemDescriptor"></param>
        private static void SetControllerActionDescriptor(XmlElement xe, PluginMenuItemDescriptor pluginMenuItemDescriptor)
        {
            ActionInfo actionDescriptor = new ActionInfo();
            if (xe.HasAttribute("nameSpace"))
            {
                actionDescriptor.NameSpace = xe.GetAttribute("nameSpace");
            }
            if (xe.HasAttribute("controller"))
            {
                actionDescriptor.ControllerName = xe.GetAttribute("controller");
            }
            if (xe.HasAttribute("action"))
            {
                actionDescriptor.ActionName = xe.GetAttribute("action");
            }
            if (xe.HasAttribute("area"))
            {
                actionDescriptor.AreaName = xe.GetAttribute("area");
            }
            if (xe.HasAttribute("requestType"))
            {
                actionDescriptor.RequestType = xe.GetAttribute("requestType");
            }
            if (xe.HasAttribute("isAjax"))
            {
                actionDescriptor.IsAjax = StringHelper.Format<bool>(xe.GetAttribute("isAjax"));
            }
            pluginMenuItemDescriptor.ActionDescriptor = actionDescriptor;
            if (xe.HasAttribute("roleIds"))
            {
                pluginMenuItemDescriptor.RoleIds = IdsToList(xe, "roleIds");
            }
            if (xe.HasAttribute("Uids"))
            {
                pluginMenuItemDescriptor.Uids = IdsToList(xe, "uids");
            }
            if (xe.HasAttribute("memberGroupIds"))
            {
                pluginMenuItemDescriptor.MemberGroupIds = IdsToList(xe, "memberGroupIds");
            }
            if (xe.HasAttribute("departmentIds"))
            {
                pluginMenuItemDescriptor.MemberGroupIds = IdsToList(xe, "departmentIds");
            }
        }

        /// <summary>
        /// ids转List
        /// </summary>
        /// <param name="xe"></param>
        /// <param name="attributeName"></param>
        /// <returns></returns>
        private static List<int> IdsToList(XmlElement xe, string attributeName)
        {
            string[] ids = StringHelper.Unique<int>(xe.GetAttribute("Uids")).Split(',');
            List<int> list = new List<int>();
            foreach (var id in ids)
            {
                list.Add(StringHelper.Format<int>(id));
            }
            return list;
        }


    }
}